package cn.ibizlab.odoo.core.odoo_maintenance.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imaintenance_stage;
import cn.ibizlab.odoo.core.odoo_maintenance.domain.Maintenance_stage;
import cn.ibizlab.odoo.core.odoo_maintenance.filter.Maintenance_stageSearchContext;
import cn.ibizlab.odoo.core.odoo_maintenance.service.IMaintenance_stageService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[保养阶段] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Maintenance_stageServiceImpl implements IMaintenance_stageService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imaintenance_stageClientService maintenance_stageClientService;

    @Override
    public boolean create(Maintenance_stage maintenance_stage) {
        boolean bOk = false ;
        Imaintenance_stage clientModel = this.convert2Model(maintenance_stage,null);
        maintenance_stageClientService.create(clientModel) ;
        this.convert2Domain(clientModel,maintenance_stage);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Maintenance_stage get(Integer id) {
        Imaintenance_stage clientModel = maintenance_stageClientService.createModel();
        clientModel.setId(id);
        maintenance_stageClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Maintenance_stage get(Maintenance_stage maintenance_stage) {
        Maintenance_stage retmaintenance_stage = this.get(maintenance_stage.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmaintenance_stage.getClass(), maintenance_stage.getClass(), false);
        copier.copy(retmaintenance_stage, maintenance_stage, null);
        return maintenance_stage ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //maintenance_stageClientService.removeBatch(maintenance_stages) ;

    }

    @Override
    public boolean update(Maintenance_stage maintenance_stage) {
        boolean bOk = false ;
        Imaintenance_stage clientModel = this.convert2Model(maintenance_stage,null);
        maintenance_stageClientService.update(clientModel) ;
        this.convert2Domain(clientModel,maintenance_stage) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Maintenance_stage> maintenance_stages, int batchSize) {

        //maintenance_stageClientService.updateBatch(maintenance_stages,batchSize) ;

    }

    @Override
    public void createBatch(List<Maintenance_stage> maintenance_stages, int batchSize) {

                        //maintenance_stageClientService.createBatch(maintenance_stages,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imaintenance_stage clientModel = maintenance_stageClientService.createModel();
        clientModel.setId(id);
        maintenance_stageClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Maintenance_stage> searchDefault(Maintenance_stageSearchContext context) {
        List<Maintenance_stage> list = new ArrayList<Maintenance_stage>();
        Page<cn.ibizlab.odoo.core.client.model.Imaintenance_stage> clientModelList = maintenance_stageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imaintenance_stage clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Maintenance_stage>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Maintenance_stage> selectByOdoo_create(Integer id){
        Maintenance_stageSearchContext context = new Maintenance_stageSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Maintenance_stage> list = new ArrayList<Maintenance_stage>();
        Page<cn.ibizlab.odoo.core.client.model.Imaintenance_stage> clientModelList = maintenance_stageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imaintenance_stage clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Maintenance_stage>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Maintenance_stage> selectByOdoo_write(Integer id){
        Maintenance_stageSearchContext context = new Maintenance_stageSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Maintenance_stage> list = new ArrayList<Maintenance_stage>();
        Page<cn.ibizlab.odoo.core.client.model.Imaintenance_stage> clientModelList = maintenance_stageClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imaintenance_stage clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Maintenance_stage>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imaintenance_stage convert2Model(Maintenance_stage domain , Imaintenance_stage model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = maintenance_stageClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getDoneDirtyFlag())
            model.setDone(domain.getDone());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getFoldDirtyFlag())
            model.setFold(domain.getFold());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Maintenance_stage convert2Domain( Imaintenance_stage model ,Maintenance_stage domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Maintenance_stage();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getDoneDirtyFlag())
            domain.setDone(model.getDone());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getFoldDirtyFlag())
            domain.setFold(model.getFold());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
